package com.ruoyi.app.controller.product.controller;

import com.ruoyi.app.controller.member.mapper.IMemberMapper;
import com.ruoyi.app.framework.base.BaseAppController;
import com.ruoyi.app.controller.member.service.IMemberService;
import com.ruoyi.app.controller.product.service.IProductCategoryService;
import com.ruoyi.common.annotation.AuthMember;
import com.ruoyi.common.annotation.ParseShopId;
import com.ruoyi.common.annotation.PassToken;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.entity.DO.member.MemberDO;
import com.ruoyi.common.entity.VO.member.MemberVO;
import com.ruoyi.common.entity.VO.product.ProductCategorySearchVO;
import com.ruoyi.common.entity.VO.product.ProductCategoryVO;
import com.ruoyi.common.entity.VO.product.ProductSearchVO;
import com.ruoyi.common.entity.VO.shop.ShopProductSearchVO;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/productCategory")
@Api(value = "商品分类", description = "商品分类包含(1油品分类,2积分商城商品分类)")
public class ProductCategoryController extends BaseAppController {

    @Resource
    private IProductCategoryService productCategoryService;

    @Resource
    private IMemberMapper memberMapper;

    @ApiOperation(value = "获取商城商品分类列表", notes = "使用场景:")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "module", value = "分类模块（1油品分类,2积分商城商品分类）", dataTypeClass = Integer.class ,required = false, paramType = "query"),
            @ApiImplicitParam(name = "appId", value = "小程序ID（不传默认为0，取一级分类和二级分类时必传）", dataTypeClass = Integer.class,required = false, paramType = "header"),
            @ApiImplicitParam(name = "start", value = "页码（不传默认1）", required = false, dataTypeClass = Integer.class,paramType = "query"),
            @ApiImplicitParam(name = "limits", value = "每页条数（不传默认1000）", required = false, dataTypeClass = Integer.class,paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = ""),
            @ApiResponse(code = 500, message = "登录超时！")
    })
    @GetMapping("/searchList")
    @PassToken
    public TableDataInfo searchList(@ApiIgnore @ParseShopId Long shopId, @ApiIgnore @AuthMember MemberDO member) {
        ProductCategorySearchVO productCategorySearchVO = new ProductCategorySearchVO();
        List<ProductCategoryVO> productCategoryList = new ArrayList<>();
        if (StringUtils.isNotEmpty(ServletUtils.getParameter("module"))) {
            productCategorySearchVO.setSystemModule(Integer.valueOf(ServletUtils.getParameter("module")));
            productCategorySearchVO.setLevel(0);
            productCategorySearchVO.setShopId(shopId);
            productCategorySearchVO.setIsEnable(1);
            productCategorySearchVO.setIsDelete(0);
            HashMap<String, Object> params = new HashMap<>();
            params.put("start", 1);
            params.put("limits", 9);
            params.put("orderBy", "parent_id asc, order_num asc");
            BaseEntity pageVo = new BaseEntity();
            pageVo.setParams(params);
            startPage(pageVo);
            productCategoryList = productCategoryService.searchList(productCategorySearchVO);
        }
        return getDataTable(productCategoryList);
    }

    @ApiOperation(value = "获取油品分类及分类下的商品", notes = "一键加油里面用到")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "小程序appID", required = true, dataTypeClass = String.class,paramType = "header")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = ""),
            @ApiResponse(code = 500, message = "登录超时！")
    })
    @PassToken
    @GetMapping("/searchPetrolTreeList")
    public TableDataInfo searchPetrolTreeList(@ApiIgnore @ParseShopId Long shopId, @ApiIgnore @AuthMember MemberDO member) {
        ShopProductSearchVO searchVO = new ShopProductSearchVO();
        if (null != member && null != member.getId()) {
            MemberVO memberInfo = memberMapper.getProfileById(member.getId());
            if (null != memberInfo) {
                searchVO.setMemberVipTypeId(memberInfo.getVipTypeId());
            }
        }
        searchVO.setIsEnable(1);
        searchVO.setIsDelete(0);
        searchVO.setShopId(shopId);
        return getDataTable(productCategoryService.searchPetrolTreeList(searchVO));
    }


}
